package logging;

import java.util.HashMap;
import java.util.Map;

import Exception.InvalidLevelException;

public enum Level {
	
	OFF(0),
	FATAL(1),
	ERROR(2),
	WARN(3),
	INFO(4),
	DEBUG(5),
	TRACE(6);
	
    private int priority;
    
    public static Map<Level, String> levels = new HashMap<Level, String>();
    static {
    	levels.put(Level.OFF, "OFF");
    	levels.put(Level.FATAL, "FATAL");
    	levels.put(Level.ERROR, "ERROR");
    	levels.put(Level.WARN, "WARN");
    	levels.put(Level.INFO, "INFO");
    	levels.put(Level.DEBUG, "DEBUG");
    	levels.put(Level.TRACE, "TRACE");
    }
    
	Level(int priority){
		this.priority = priority;
	}
	
	/*
	 * return the priority associated with a level
	 */
	public int getPriority(){
		return this.priority;
	}
	
	/*
	 * return level name
	 */
	public static String LevelToString(Level level){
		return levels.get(level);
	}
	
	/*
	 * return level associated with a string. 
	 */
	public static Level stringToLevel(String levelName) throws InvalidLevelException{
		for (Map.Entry<Level, String> values : levels.entrySet()){
			if (levelName.toUpperCase().equals(values.getValue()))
				//return values.getKey();
				return Level.valueOf(values.getValue());
		}
		
		throw new InvalidLevelException(levelName);
	}
	
	/* compara el nivel actual con otro nivel y si la prioridad es mayor o
	 * igual a la prioridad del otro nivel entonces se rotorna un cero, sino -1
	 */
	public int compareWith(Level otherLevel){
		if (getPriority() >= otherLevel.getPriority())
			return 0;
		return -1;
	}
	
}
